
#include <stdio.h>
#include <GL/glut.h>

#include <Misc.hpp>
#include <Player.hpp>
#include <Piece.hpp>
#include <Board.hpp>

typedef struct {

	float r;
	float g;
	float b;
} RGB;

void exibeCB(void);
void redesenhaCB(int w, int h);
void inic(void);
void GerenciaTeclado(unsigned char key, int x, int y);
float x = 272.0;
float y = 65.0;


RGB bola_cor = {1, 1, 0};


int main(int argc, char *argv[]) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
	inic();
	glutKeyboardFunc(GerenciaTeclado);
	glutMainLoop();
	return 1;
}

/*
 *--------------------------------------------------
 * Esta funcao inicializa a janela OpenGL, registra
 * as rotinas de callback e inicializa as matrizes
 * do OpenGL
 *--------------------------------------------------
 */
void inic(void) {

	glutInitWindowSize(1050, 680);
	glutInitWindowPosition(130, 34);
	glutCreateWindow("Minha dama das ferias =P");

	glutDisplayFunc(exibeCB);
	glutReshapeFunc(redesenhaCB);

	glClearColor(255.0, 255.0, 255.0, 0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	/* Limpa buffer de cor */
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(127.0, 127.0, 127.0);
}


//Desenha texto na janela
void RenderString(float x, float y, void *font, const char* string, const RGB rgb) {

	glPushMatrix();
		glColor3f(rgb.r, rgb.g, rgb.b);
		// posicao
		glRasterPos2f(x, y);
		// Exibe caracter a caracter
		while(*string)
			 glutBitmapCharacter(font, *string++);
	glPopMatrix();
}


void exibeCB(void) {

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClear(GL_COLOR_BUFFER_BIT);

	RGB r = {0, 0, 0};
	RenderString(420.0f, 620.0f, GLUT_BITMAP_TIMES_ROMAN_24, "APNG CHECKERS", r);

	//FILEIRA NUMERO 01: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(310, 100);
		glVertex2i(310, 30);
		glVertex2i(240, 30);
		glVertex2i(240, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(380, 100);
		glVertex2i(380, 30);
		glVertex2i(310, 30);
		glVertex2i(310, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(450, 100);
		glVertex2i(450, 30);
		glVertex2i(380, 30);
		glVertex2i(380, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(520, 100);
		glVertex2i(520, 30);
		glVertex2i(450, 30);
		glVertex2i(450, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(590, 100);
		glVertex2i(590, 30);
		glVertex2i(520, 30);
		glVertex2i(520, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(660, 100);
		glVertex2i(660, 30);
		glVertex2i(590, 30);
		glVertex2i(590, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(730, 100);
		glVertex2i(730, 30);
		glVertex2i(660, 30);
		glVertex2i(660, 100);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(800, 100);
		glVertex2i(800, 30);
		glVertex2i(730, 30);
		glVertex2i(730, 100);
	glEnd();

	//FILEIRA NUMERO 01: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
	glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(310, 170);
		glVertex2i(310, 100);
		glVertex2i(240, 100);
		glVertex2i(240, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(380, 170);
		glVertex2i(380, 100);
		glVertex2i(310, 100);
		glVertex2i(310, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(450, 170);
		glVertex2i(450, 100);
		glVertex2i(380, 100);
		glVertex2i(380, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(520, 170);
		glVertex2i(520, 100);
		glVertex2i(450, 100);
		glVertex2i(450, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(590, 170);
		glVertex2i(590, 100);
		glVertex2i(520, 100);
		glVertex2i(520, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(660, 170);
		glVertex2i(660, 100);
		glVertex2i(590, 100);
		glVertex2i(590, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(730, 170);
		glVertex2i(730, 100);
		glVertex2i(660, 100);
		glVertex2i(660, 170);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(800, 170);
		glVertex2i(800, 100);
		glVertex2i(730, 100);
		glVertex2i(730, 170);
	glEnd();

	//FILEIRA NUMERO 02: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(310, 240);
		glVertex2i(310, 170);
		glVertex2i(240, 170);
		glVertex2i(240, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(380, 240);
		glVertex2i(380, 170);
		glVertex2i(310, 170);
		glVertex2i(310, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(450, 240);
		glVertex2i(450, 170);
		glVertex2i(380, 170);
		glVertex2i(380, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(520, 240);
		glVertex2i(520, 170);
		glVertex2i(450, 170);
		glVertex2i(450, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(590, 240);
		glVertex2i(590, 170);
		glVertex2i(520, 170);
		glVertex2i(520, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(660, 240);
		glVertex2i(660, 170);
		glVertex2i(590, 170);
		glVertex2i(590, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(730, 240);
		glVertex2i(730, 170);
		glVertex2i(660, 170);
		glVertex2i(660, 240);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(800, 240);
		glVertex2i(800, 170);
		glVertex2i(730, 170);
		glVertex2i(730, 240);
	glEnd();

	//FILEIRA NUMERO 03: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(310, 310);
		glVertex2i(310, 240);
		glVertex2i(240, 240);
		glVertex2i(240, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(380, 310);
		glVertex2i(380, 240);
		glVertex2i(310, 240);
		glVertex2i(310, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(450, 310);
		glVertex2i(450, 240);
		glVertex2i(380, 240);
		glVertex2i(380, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(520, 310);
		glVertex2i(520, 240);
		glVertex2i(450, 240);
		glVertex2i(450, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(590, 310);
		glVertex2i(590, 240);
		glVertex2i(520, 240);
		glVertex2i(520, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(660, 310);
		glVertex2i(660, 240);
		glVertex2i(590, 240);
		glVertex2i(590, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(730, 310);
		glVertex2i(730, 240);
		glVertex2i(660, 240);
		glVertex2i(660, 310);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(800, 310);
		glVertex2i(800, 240);
		glVertex2i(730, 240);
		glVertex2i(730, 310);
	glEnd();

	//FILEIRA NUMERO 04: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(310, 380);
		glVertex2i(310, 310);
		glVertex2i(240, 310);
		glVertex2i(240, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(380, 380);
		glVertex2i(380, 310);
		glVertex2i(310, 310);
		glVertex2i(310, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(450, 380);
		glVertex2i(450, 310);
		glVertex2i(380, 310);
		glVertex2i(380, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(520, 380);
		glVertex2i(520, 310);
		glVertex2i(450, 310);
		glVertex2i(450, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(590, 380);
		glVertex2i(590, 310);
		glVertex2i(520, 310);
		glVertex2i(520, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(660, 380);
		glVertex2i(660, 310);
		glVertex2i(590, 310);
		glVertex2i(590, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(730, 380);
		glVertex2i(730, 310);
		glVertex2i(660, 310);
		glVertex2i(660, 380);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(800, 380);
		glVertex2i(800, 310);
		glVertex2i(730, 310);
		glVertex2i(730, 380);
	glEnd();

	//FILEIRA NUMERO 05: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(310, 450);
		glVertex2i(310, 380);
		glVertex2i(240, 380);
		glVertex2i(240, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(380, 450);
		glVertex2i(380, 380);
		glVertex2i(310, 380);
		glVertex2i(310, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(450, 450);
		glVertex2i(450, 380);
		glVertex2i(380, 380);
		glVertex2i(380, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(520, 450);
		glVertex2i(520, 380);
		glVertex2i(450, 380);
		glVertex2i(450, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(590, 450);
		glVertex2i(590, 380);
		glVertex2i(520, 380);
		glVertex2i(520, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(660, 450);
		glVertex2i(660, 380);
		glVertex2i(590, 380);
		glVertex2i(590, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(730, 450);
		glVertex2i(730, 380);
		glVertex2i(660, 380);
		glVertex2i(660, 450);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(800, 450);
		glVertex2i(800, 380);
		glVertex2i(730, 380);
		glVertex2i(730, 450);
	glEnd();

	//FILEIRA NUMERO 06: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(310, 520);
		glVertex2i(310, 450);
		glVertex2i(240, 450);
		glVertex2i(240, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(380, 520);
		glVertex2i(380, 450);
		glVertex2i(310, 450);
		glVertex2i(310, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(450, 520);
		glVertex2i(450, 450);
		glVertex2i(380, 450);
		glVertex2i(380, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(520, 520);
		glVertex2i(520, 450);
		glVertex2i(450, 450);
		glVertex2i(450, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(590, 520);
		glVertex2i(590, 450);
		glVertex2i(520, 450);
		glVertex2i(520, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(660, 520);
		glVertex2i(660, 450);
		glVertex2i(590, 450);
		glVertex2i(590, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(730, 520);
		glVertex2i(730, 450);
		glVertex2i(660, 450);
		glVertex2i(660, 520);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(800, 520);
		glVertex2i(800, 450);
		glVertex2i(730, 450);
		glVertex2i(730, 520);
	glEnd();

	//FILEIRA NUMERO 07: COMECANDO DE BAIXO
	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(310, 590);
		glVertex2i(310, 520);
		glVertex2i(240, 520);
		glVertex2i(240, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(380, 590);
		glVertex2i(380, 520);
		glVertex2i(310, 520);
		glVertex2i(310, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(450, 590);
		glVertex2i(450, 520);
		glVertex2i(380, 520);
		glVertex2i(380, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(520, 590);
		glVertex2i(520, 520);
		glVertex2i(450, 520);
		glVertex2i(450, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(590, 590);
		glVertex2i(590, 520);
		glVertex2i(520, 520);
		glVertex2i(520, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(660, 590);
		glVertex2i(660, 520);
		glVertex2i(590, 520);
		glVertex2i(590, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(730, 590);
		glVertex2i(730, 520);
		glVertex2i(660, 520);
		glVertex2i(660, 590);
	glEnd();

	glBegin(GL_QUADS);
		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2i(800, 590);
		glVertex2i(800, 520);
		glVertex2i(730, 520);
		glVertex2i(730, 590);
	glEnd();

	glColor3f(bola_cor.r, bola_cor.g, bola_cor.b);
	glTranslatef(x, y, -99);
	glutSolidSphere(100, 50, 50);

	glFlush();
}


//Funcao para eventos de teclado
void GerenciaTeclado(unsigned char key, int xf, int yf) {

    switch (key) {
            case 'a':
            	if (y < 490) {
            		x += 70;
            		y += 70;
            	}
                    break;
            case 'b':
            	if (x < 710) {
            		x += 70;
            	}
                 break;
            case 'c':
            	if (y < 490) {
            		y += 70;
            	}
            	break;
    }
    glutPostRedisplay();
}

/*
 *--------------------------------------------------
 * Rotina para tratamento de evento
 * Alteracao de tamanho da janela
 * w - largura corrente da janela
 * h - altura corrente da janela
 *--------------------------------------------------
 */
void redesenhaCB(int w, int h){

	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, (GLfloat) w, 0, (GLfloat) h , -1.0, 1.0);

}
